package com.android.clinic.app.http;

import com.android.clinic.app.BuildConfig;
import com.android.clinic.app.ClinicApplication;
import com.android.clinic.app.http.converter.FastJsonConverterFactory;
import com.android.clinic.app.http.interceptor.CacheInterceptor;
import com.android.clinic.app.http.interceptor.ParamsInterceptor;
import com.ihsanbal.logging.Level;
import com.ihsanbal.logging.LoggingInterceptor;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.internal.platform.Platform;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * Created by cheng on 2018/2/7.
 */
public class HttpClient {

    private static String PRODUCE = "http://47.97.17.240:";  // 生产环境
    private static String TEST = "http://39.105.11.230:";  // 测试环境

    private static String BASE_URL = PRODUCE + "8080/";

    private static HttpService httpService;
    private static Retrofit retrofit;
    private static final int DEFAULT_TIMEOUT = 10;

    /**
     * @return retrofit的底层利用反射的方式, 获取所有的api接口的类
     */
    public static HttpService getHttpService() {
        if (httpService == null) {
            httpService = getRetrofit().create(HttpService.class);
        }
        return httpService;
    }

    private static Retrofit getRetrofit() {
        if (retrofit == null) {
            synchronized (HttpClient.class) {
                if (retrofit == null) {
                    //设置 请求的缓存的大小跟位置
                    File cacheFile = new File(ClinicApplication.cacheDir, "/NetCache");
                    Cache cache = new Cache(cacheFile, 1024 * 1024 * 50); //50Mb 缓存的大小

                    LoggingInterceptor loggingInterceptor = new LoggingInterceptor.Builder().loggable(BuildConfig.DEBUG)
                            .setLevel(Level.BASIC)
                            .log(Platform.INFO)
                            .request("Request")
                            .response("Response")
                            .addHeader("version", BuildConfig.VERSION_NAME)
                            .addQueryParam("query", "0").build();

                    OkHttpClient client = new OkHttpClient
                            .Builder()
                            .addInterceptor(loggingInterceptor) //日志,所有的请求响应度看到
                            .cache(cache)
                            .addInterceptor(new CacheInterceptor())  //添加缓存
                            .addInterceptor(new ParamsInterceptor())
                            .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                            .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                            .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                            .build();

                    // 获取retrofit的实例
                    retrofit = new Retrofit
                            .Builder()
                            .baseUrl(BASE_URL)  //域名地址
                            .client(client)
//                            .addConverterFactory(GsonConverterFactory.create())
                            .addConverterFactory(FastJsonConverterFactory.create())
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .build();
                }
            }
        }
        return retrofit;
    }

}
